class Solution:
    def minFlips(self, s: str) -> int:
        # 偶数位、奇数位的1的数量
        n0, n1 = 0, 0
        for i in range(len(s)):
            if s[i] == "1":
                if i % 2 == 1:
                    n1 += 1
                else:
                    n0 += 1

        # 计算偶数位、奇数位的总计位数
        t0 = (len(s) + 1) // 2
        t1 = len(s) // 2

        ans = min(n0 + (t1 - n1), n1 + (t0 - n0))

        for i in range(len(s)):
            if s[i] == "1":
                n0, n1 = n1, n0 - 1
                if len(s) % 2 == 0:
                    n1 += 1
                else:
                    n0 += 1
            else:
                n0, n1 = n1, n0
            ans = min(ans, n0 + (t1 - n1), n1 + (t0 - n0))

        return ans


if __name__ == "__main__":
    print(Solution().minFlips("111000"))  # 2
    print(Solution().minFlips("010"))  # 0
    print(Solution().minFlips("1110"))  # 1

    # 自制用例
    print(Solution().minFlips("11110"))  # 1
    print(Solution().minFlips("1010010"))  # 0
